package com.hamizhong.iflowers.web.action;

import java.util.Date;

import javax.servlet.http.Cookie;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.ICityService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IOrdersService;
import com.hamizhong.iflowers.service.IProvinceService;
import com.hamizhong.iflowers.service.IUserService;
import com.hamizhong.iflowers.utils.MyColumnIndicator;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CartGoodsVO;
import com.hamizhong.iflowers.web.vo.CityVO;
import com.hamizhong.iflowers.web.vo.GoodsVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.OrdersVO;
import com.hamizhong.iflowers.web.vo.ProvinceVO;
import com.hamizhong.iflowers.web.vo.UserVO;

public class AjaxAddOrdersAction extends MyBaseAjaxAction {

	private IOrdersService iOrdersService = null;
	private IProvinceService iProvinceService = null;
	private ICityService iCityService = null;
	private IGoodsService iGoodsService = null;
	private IUserService iUserService = null;
	
	public IOrdersService getiOrdersService() {
		return iOrdersService;
	}
	public void setiOrdersService(IOrdersService iOrdersService) {
		this.iOrdersService = iOrdersService;
	}
	public IProvinceService getiProvinceService() {
		return iProvinceService;
	}
	public void setiProvinceService(IProvinceService iProvinceService) {
		this.iProvinceService = iProvinceService;
	}
	public ICityService getiCityService() {
		return iCityService;
	}
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxAddOrdersAction...");

		String consigneeName = getRQ().getParameter("consigneeName");
		String consigneePhone = getRQ().getParameter("consigneePhone");
		String consigneeAddress = getRQ().getParameter("consigneeAddress");
		String zip = getRQ().getParameter("zip");
		String selectCityId = getRQ().getParameter("selectCityId");
		String cartGoodsIdS = getRQ().getParameter("cartGoodsIdS");
		String cartGoodsCntS = getRQ().getParameter("cartGoodsCntS");
		String remarks = getRQ().getParameter("remarks");
		
		logger.info("consigneeName"+consigneeName);
		logger.info("consigneePhone"+consigneePhone);
		logger.info("consigneeAddress"+consigneeAddress);
		logger.info("zip"+zip);
		logger.info("remarks"+remarks);
		logger.info("selectCityId"+selectCityId);
		logger.info("cartGoodsIdS"+cartGoodsIdS);
		logger.info("cartGoodsCntS"+cartGoodsCntS);
		
		String consigneeCompleteAddress = "";
		String[] cartGoodsIdArr = cartGoodsIdS.split("-");
		String[] cartGoodsCntArr = cartGoodsCntS.split("-");
		int orderCnts = cartGoodsIdArr.length;
		
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj;
		
		UserVO userVO = (UserVO) getSSN().getAttribute(MyConstants.IFLOWERS_LOGIN_USER_VO);
		if(userVO == null) {
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			jo.put("msg", "对不起，登录信息已失效，请刷新后重试！");
			return jo;
		}
		
		OrdersVO ordersVO = null;
		CityVO cityVO = null;
		ProvinceVO provinceVO = null;
		Float ordersTotalPrice;

		Date date = new Date();
		Cookie cartGoodsArrCookie = null;
		String cartGoodsArrStr = "";
		int successOrdersCnt = 0;
		
		try {
			if(orderCnts == 0) {
				jo.put("result", MyConstants.AJAX_FAILED_ALERT);
				jo.put("msg", "对不起，当前订单没有有效商品信息！");
				return jo;
			}

			for(int i=0;i<orderCnts;i++) {
				IOPResultDto<IObjectVO> dto = iGoodsService.findById(Integer.valueOf(cartGoodsIdArr[i]));
				if(!dto.isSuccess()) {
					jo.put("result", MyConstants.AJAX_FAILED_ALERT);
					jo.put("msg", "系统忙，请刷新后重试！");
					return jo;
				}
				GoodsVO goodsVOTemp = dto.getFirstResult().getGoodsVOList().get(0);
				if(goodsVOTemp.getState() != MyColumnIndicator.G_ST_B) {
					continue;
				}
				
				IOPResultDto<IObjectVO> dtoCity = iCityService.findById(Integer.valueOf(selectCityId));
				cityVO = dtoCity.getFirstResult().getCityVOList().get(0);
				provinceVO = cityVO.getProvinceVO();
				
				consigneeCompleteAddress = provinceVO.getProvinceName() + cityVO.getCityName() + consigneeAddress;
				ordersTotalPrice = Float.valueOf(goodsVOTemp.getGoodsPrice().toString()) * (Integer.valueOf(cartGoodsCntArr[i]));
				
				ordersVO = new OrdersVO();
				ordersVO.setOrderId(0);
				ordersVO.setConsigneeName(consigneeName);
				ordersVO.setConsigneePhone(consigneePhone);
				ordersVO.setConsigneeAddr(consigneeCompleteAddress);
				ordersVO.setGoodsVO(goodsVOTemp);
				ordersVO.setUserVO(userVO);
				ordersVO.setCityVO(cityVO);
				String orderNum = MyTools.formatDttm2YYYYMMDD(date)+MyTools.query10LengthRandomNumStr();
				ordersVO.setOrderNum(orderNum);
				ordersVO.setGoodsCnt(Integer.valueOf(cartGoodsCntArr[i]));
				ordersVO.setOrderPrice(ordersTotalPrice);
				ordersVO.setZip(zip);
				ordersVO.setRemarks(remarks);
				ordersVO.setOrderState(MyColumnIndicator.O_ST_A);
				ordersVO.setCreateDate(date);
				ordersVO.setDoneDate(date);
				
				IOPResultDto<IObjectVO> resultDto = iOrdersService.addOrders(ordersVO);
				if(!resultDto.isSuccess()) {
					jo.put("result", MyConstants.AJAX_FAILED_ALERT);
					jo.put("msg", resultDto.getErrorMSG());
					return jo;
				}
				++successOrdersCnt;
			}
			
			if(successOrdersCnt == 0) {
				jo.put("result", MyConstants.AJAX_FAILED_ALERT);
				jo.put("msg", "对不起，没有生成任何有效订单，请刷新后重试！");
				return jo;
			}
			
			Cookie[] cookie = getRQ().getCookies();
			if(cookie != null) {
				for(int i=0;i<cookie.length;i++) {
					String res = cookie[i].getName();
					if(res.equals("client_cart_product_arr")) {
						cartGoodsArrCookie = cookie[i];
						cartGoodsArrStr = cookie[i].getValue();
						break;
					}
				}
			}
			if(cartGoodsArrStr != "") {
				cartGoodsArrCookie.setMaxAge(0);
				getRP().addCookie(cartGoodsArrCookie);
			}
			
			jo.put("result", MyConstants.AJAX_SUCCESS);
			jo.put("msg", "订单创建成功，共拆分成了"+successOrdersCnt+"个子订单！");
			return jo;
			
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			jo.put("msg", "系统忙，请刷新后重试！");
			return jo;
		}
	}

}
